Hybrid Image
Published:
1. Project #1 - Text Classification
- Goal: Train a model to classifiy positive and negative movie reviews.
- Methods: In three different ways:
- perceptron algorithm
- Methods: In three different ways:
How to create hybrid image using image filtering
1. Main idea
- Hybrid image: an image with chaning perception based on distance.
- Mechanism: Higher frequency survives in longer distance.
2. Related math
- covariance matrix 协方差矩阵: if we examine N-dimensional samples (N rows), the Cij element in matrix will be the covariance of the row i and row j.
- covariance 协方差: a metric showing how “correlated” the two variables are, or the tendency in the linear relationship between the two. If the covx,y=0, the plot of points of the two vairable will has a shape of circle. If covx,y>0, if will go “direct proportional”.
3. Gaussian Filter 高斯滤波
- 1D Gaussian kernel 一维高斯核函数:
- a. What: a (k, 1) vector
- b. How:
- 1D Gaussian kernel 一维高斯核函数:
- 2D Gaussian kernel 二维高斯核函数:
- a. What: a (?, ?) matrix.
- b. How: Multi-dimensional Gaussian functions are separable, that is they can be calculated as the product of the Gaussian function along all axes. For the 2-d case it means:
terms[x, y] = terms[x]*terms[y]
. Thus, we can create 2D kernel byaxis = np.array(create_Gaussian_kernel_1D(k, sigma))
andkernel = axis * axis.T
- 2D Gaussian kernel 二维高斯核函数:
- 2D convolution 二维卷积 ref
- a. What:
- b: How:
- (1). Calculate padding to preserve dimension: In many cases, we will want to set ph=kh−1 and pw=kw−1 to give the input and output the same height and width
- 2D convolution 二维卷积 ref
4. Frequency Filter - High pass vs low pass 高通滤波器 和 低通滤波器
- Low pass:
- a. What: image with only the low frequencies
- b. How: apply 2D convolution using the Gaussian kernel. Then do clipping of the image.
- Low pass:
- High pass:
- a. What: image with only the high frequencies
- b. How: image - low_pass. Then do clipping of the image.
- High pass:
- Hybrid image:
- a. What: image with both low pass and high pass of two different sources.
- b. How: hybrid = low_pass + high_pass. Then do the clipping.
- Hybrid image:
- Clipping:
- a. What: normalize each pixel value to range 0 to 1.
- b. How:
- (1)
np.clip(image, 0, 1)
- (2)
hybrid_img_min = hybrid_image_unclip.min()
hybrid_img_max = hybrid_image_unclip.max()
hybrid_image = (hybrid_image_unclip - hybrid_img_min) / (hybrid_img_max - hybrid_img_min)
.
- Clipping:
- Cutoff frequency:
- a. What is the range?
- b. How is it related to standard deviation: (1) narrower Gaussian = higher cutoff (2) wider Gaussian = lower cutoff
- Cutoff frequency:
4. Pytorch
- Convert 2D kernel into Pytorch kernels with channels:
- a. What: from (k, k) with float to (c, 1, k, k) with tensor.
- b. How:
- (1).
k1 = np.reshape(k1, (1, 1, kernel.shape[0], kernel.shape[1]))
- (2).
k2 = np.tile(k1, (self.n_channels, 1, 1, 1)) # repeat n_channels of times on the axis dim of 0
- (3).
new_kernel = torch.Tensor(k2)
- Convert 2D kernel into Pytorch kernels with channels: